package cn.tedu.cinema.member.service.impl;


import cn.tedu.cinema.member.mapper.MemberMapper;
import cn.tedu.cinema.member.pojo.dto.MemberQuery;
import cn.tedu.cinema.member.pojo.entity.Member;
import cn.tedu.cinema.member.pojo.vo.MemberVO;
import cn.tedu.cinema.member.service.MemberService;

import cn.tedu.cinema.user.pojo.vo.UserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/* 业务层实现类需要添加@Service注解,表示该类为业务层实现类 */
@Slf4j
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberMapper memberMapper;
    @Override
    public PageInfo<MemberVO> selectUser(int pageNum, int pageSize, MemberQuery memberQuery) {
        log.debug("查询用户业务,参数:{}", memberQuery);
        PageHelper.startPage(pageNum, pageSize);
        Member member1 = new Member();
        List<MemberVO> list = memberMapper.selectMember(pageNum, pageSize, memberQuery);
        
        for (MemberVO member : list) {
            if (member != null && member.getExpirationDate() != null) {
                LocalDateTime expirationDateTime = member.getExpirationDate().toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime();
                LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
                
                member1.setId(member.getId());
                member1.setMemberLevel(member.getMemberLevel());
                member1.setDiscount(member.getDiscount());
                
                if (now.isAfter(expirationDateTime)) {
                    member1.setStatus("已过期");
                    member1.setMemberLevel("普通会员");
                    member1.setDiscount(1.0);
                    memberMapper.update(member1);
                } else {
                    member1.setStatus("未过期");
                    memberMapper.update(member1);
                }
                log.debug("更新前会员信息: {}", member1);
                log.debug("会员信息更新完成");
            }
        }
        
        PageInfo<MemberVO> pageInfo = new PageInfo<>(list);
        log.debug("查询用户业务,参数:{}", pageInfo);
        return pageInfo;
    }

    @Override
    public void saveMember(String userId, String memberLevel) {
        log.debug("更新会员等级:用户ID={}, 会员等级={}", userId, memberLevel);
        
        Member member = new Member();
        member.setUserId(userId);
        member.setMemberLevel(memberLevel);
        // 查询用户是否已经是会员
        Member existingMember = memberMapper.findByUserId(userId);
        switch (memberLevel) {
            case "青铜":
                member.setDiscount(0.95); // 95折
                break;
            case "白银":
                member.setDiscount(0.90); // 90折
                break;
            case "黄金":
                member.setDiscount(0.85); // 85折
                break;
            case "钻石":
                member.setDiscount(0.80); // 80折
                break;
            case "王者":
                member.setDiscount(0.75); // 75折
                break;
            default:
                member.setDiscount(1.0); // 默认不打折
                break;
        }

        // 设置注册时间和��期时间
        LocalDateTime now = LocalDateTime.now();
        member.setRegistrationDate(Date.from(now.atZone(ZoneId.systemDefault()).toInstant()));
        
        // 设置一年后过期
        LocalDateTime expirationDateTime = now.plusYears(1);
        member.setExpirationDate(Date.from(expirationDateTime.atZone(ZoneId.systemDefault()).toInstant()));
        member.setStatus("未过期");

        if (existingMember != null) {
            // 如果已经是会员，更新会员信息
            log.debug("更新已存在的会员信息");
            memberMapper.updateMemberLevelByUserId(member);
        } else {
            // 如果不是会员，插入新会员信息
            log.debug("插入新会员信息");
            memberMapper.insertMember(member);
        }
    }


}







